package com.jinzhuo.music.controller;

import com.alibaba.fastjson.JSONObject;
import com.jinzhuo.music.domain.Rank;
import com.jinzhuo.music.domain.Song;
import com.jinzhuo.music.service.RankService;
import com.jinzhuo.music.service.SongService;
import com.jinzhuo.music.utils.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * 评价分controller
 */
@RestController
@RequestMapping("/rank")
public class RankController {
    @Autowired
    private RankService rankService;
    /**
     * 添加评价
     */
    @RequestMapping(value = "addRank",method = RequestMethod.POST)
    public Object addRank(HttpServletRequest request){
        JSONObject object = new JSONObject();
        String songListId = request.getParameter("songListId");
        String consumerId = request.getParameter("consumerId");
        String score = request.getParameter("score");

        //保存到歌手的对象中
        Rank rank = new Rank();
        rank.setConsumerId(Integer.parseInt(consumerId));
        rank.setSongListId(Integer.parseInt(songListId));
        rank.setScore(Integer.parseInt(score));
        boolean flag = rankService.addRank(rank);
        if (flag){
            object.put(Consts.CODE,"1");
            object.put(Consts.MSG,"评价成功");
        }else {
            object.put(Consts.CODE,0);
            object.put(Consts.MSG,"评价失败");
        }
        return object;
    }

    /**
     * 查歌单平均分
     * @param request
     * @return
     */
    @RequestMapping(value = "getAvgScore",method = RequestMethod.POST)
    public Object getAvgScore(HttpServletRequest request){
        String songListId = request.getParameter("songListId");
        int score = rankService.getAvgScore(Integer.parseInt(songListId));
        return score;
    }
}
